package com.giveu.master.worker;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;


/**
 * Hello world!
 *
 */
public class App 
{
	public static void main(String[] args) throws IOException {  
//		//固定使用4个Workde  
//		Master master = new Master(new DownloadWorker(), 4);  
//		for(int i=1; i<=100; i++) //提交100个子任务  
//			master.submit(i);  
//		master.execute(); //开始计算  
//
//		Map<String, Object> resultMap = master.getResultMap();  
//
//		int re = 0;  //最终计算结果保存在此  
//		//不需要等待所有Worker都执行完即可  
//		while(true) {  
//			Set<String> keys = resultMap.keySet();  //开始计算最终结果  
//			String key = null;  
//			for(String k : keys) {  
//				key = k;  
//				break;  
//			}  
//			Integer i = null;  
//			if(key != null)  
//				i = (Integer)resultMap.get(key);  
//			if(i != null)  
//				re += i; //最终结果  
//				if(key != null)  
//					resultMap.remove(key); //移除已被计算过的项目  
//				if(master.isComplete() && resultMap.size()==0)  
//					break;  
//		}  
//		System.out.println(re);  
		
		String urls = "http://10.12.11.126:8180/PrimaryRecordings_epro12/EPro/Contact/1/;http://10.12.11.126:8180/PrimaryRecordings_epro11/EPro/Contact/1/";
    	byte[] base = getDownloadWorker(urls, "100237580.vox");
    	System.out.println(base.length);
    	
	}  
	
	
	private static byte[] getDownloadWorker(String urls, String fileName) {
    	byte[] base = null;
    	Master master = new Master(new DownloadWorker(), 4);  
    	String[] srcs = urls.split(";");
		for(int i=0; i<srcs.length; i++){
			master.submit(srcs[i]+fileName);
		}
		master.execute();
		Map<String, Object> resultMap = master.getResultMap();  
		while(true) {  
			Set<String> keys = resultMap.keySet();
			String key = null;  
			for(String k : keys) {  
				key = k;  
				break;  
			}  
			base = null;  
			if(key != null)  
				base = (byte[])resultMap.get(key);  
			if(base != null)  
				break;
			if(key != null)  
				resultMap.remove(key);
			if(master.isComplete() && resultMap.size()==0)  
				break;  
		}  
		return base;
	}
	
	public static byte[] getRecord(String srcUrl) throws IOException{
        byte[] data = null;
        URL url = new URL(srcUrl);
        HttpURLConnection http = (HttpURLConnection) url.openConnection();
        //http.setConnectTimeout(5000);
        //http.setReadTimeout(5000);
        http.setRequestMethod("GET");
        //http.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
        //http.setRequestProperty("Connection", "keep-alive");
        http.connect();
        int status = http.getResponseCode();
        InputStream in = null;
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int len = 0;
        try{
            if(status == 200){
                in = http.getInputStream();
                while((len = in.read(buffer)) != -1){
                    bout.write(buffer, 0, len);
                }
                bout.flush();
                data = bout.toByteArray();
            }
        }catch (IOException e) {
            throw e;
        } finally {
            if(bout != null) bout.close();
        }
        return data;
    }
}
